package com.lzy.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzy.base.BaseInfoProperties;
import com.lzy.mapper.CommentMapper;
import com.lzy.mapper.FriendCircleLikedMapper;
import com.lzy.mapper.FriendCircleMapper;
import com.lzy.mapper.custom.CommentMapperCustom;
import com.lzy.mapper.custom.FriendCircleMapperCustom;
import com.lzy.pojo.Comment;
import com.lzy.pojo.FriendCircle;
import com.lzy.pojo.FriendCircleLiked;
import com.lzy.pojo.Users;
import com.lzy.pojo.bo.CommentBO;
import com.lzy.pojo.bo.FriendCircleBO;
import com.lzy.pojo.vo.CommentVO;
import com.lzy.pojo.vo.FriendCircleVO;
import com.lzy.service.CommentService;
import com.lzy.service.FriendCircleService;
import com.lzy.service.UsersService;
import com.lzy.utils.PagedGridResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author lzy
 * @since 2025-08-26
 */
@Service
public class CommentServiceImpl extends BaseInfoProperties implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UsersService usersService;

    @Autowired
    private CommentMapperCustom commentMapperCustom;

    @Transactional
    @Override
    public CommentVO createComment(CommentBO commentBO) {

        // 新增评论
        Comment pendingComment = new Comment();
        BeanUtils.copyProperties(commentBO,pendingComment);
        pendingComment.setCreatedTime(LocalDateTime.now());
        commentMapper.insert(pendingComment);

        // 留言后的数据需要返回给前端，进行扩展
        CommentVO commentVO = new CommentVO();
        BeanUtils.copyProperties(pendingComment,commentVO);
        Users user = usersService.getById(commentBO.getCommentUserId());
        commentVO.setCommentUserNickname(user.getNickname());
        commentVO.setCommentUserFace(user.getFace());
        commentVO.setCommentId(pendingComment.getId());

        return commentVO;
    }

    @Override
    public List<CommentVO> queryAll(String friendCircleId) {
        Map<String,Object> map = new HashMap<>();
        map.put("friendCircleId",friendCircleId);
        List<CommentVO> commentVOList = commentMapperCustom.queryFriendCircleComments(map);
        return commentVOList;
    }

    @Transactional
    @Override
    public void deleteComment(String commentUserId, String commentId, String friendCircleId) {
        QueryWrapper<Comment> deleteWrapper = new QueryWrapper<Comment>()
                .eq("comment_user_id",commentUserId)
                .eq("id",commentId)
                .eq("friend_circle_id",friendCircleId);
        commentMapper.delete(deleteWrapper);
    }
}
